package com.xiyu.service.service.area;

import com.xiyu.service.vo.area.officeAreaClass.OfficeAreaClassListQueryOutput;
import com.xiyu.service.vo.area.officeAreaClass.OfficeAreaClassSingleGetOutput;
import com.xiyu.service.vo.area.officeAreaClass.OfficeAreaClassUpdateInput;
import com.xiyu.service.model.office.area.OfficeAreaClass;
import com.xiyu.service.repository.office.area.OfficeAreaClassRepository;
import com.xiyu.service.vo.area.officeAreaClass.OfficeAreaClassCreateInput;
import com.xiyu.service.vo.area.officeAreaClass.OfficeAreaClassPageQueryOutput;
import com.xiyu.service.vo.area.officeAreaClass.OfficeAreaClassPageQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.util.entity.EntityUtils;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.area.OfficeAreaClassConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.area.OfficeAreaClassErrorCode.*;

/**
 * 办公区分类 Service 实现类
 */
@Service
@Validated
public class OfficeAreaClassServiceImpl implements OfficeAreaClassService {

    @Resource
    private OfficeAreaClassRepository officeAreaClassRepository;




    @Override
    public PageResult<OfficeAreaClassPageQueryOutput> pageQuery(OfficeAreaClassPageQueryInput inputVO) {
        Page<OfficeAreaClass> pageOfficeAreaClass = officeAreaClassRepository.pageQuery(inputVO);
        List<OfficeAreaClassPageQueryOutput> listOfficeAreaClass = OfficeAreaClassConvert.INSTANCE.pageQueryOutputConvert(pageOfficeAreaClass.getRows());
        return new PageResult<>(listOfficeAreaClass, pageOfficeAreaClass.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(OfficeAreaClassCreateInput inputVO) {
        OfficeAreaClass newOfficeAreaClass = OfficeAreaClassConvert.INSTANCE.createInputConvert(inputVO);
        newOfficeAreaClass = officeAreaClassRepository.insert(newOfficeAreaClass);
        return newOfficeAreaClass.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(OfficeAreaClassUpdateInput inputVO) {
        Optional<OfficeAreaClass> optionalOldOfficeAreaClass = officeAreaClassRepository.findById(inputVO.getId());
        if(optionalOldOfficeAreaClass.isEmpty()){
            throw exception(OFFICE_AREA_CLASS_NOT_EXIST);
        }
        OfficeAreaClass updateOfficeAreaClass = OfficeAreaClassConvert.INSTANCE.updateInputConvert(inputVO);
        if (!EntityUtils.isEquals(optionalOldOfficeAreaClass.get(), updateOfficeAreaClass))
            officeAreaClassRepository.update(updateOfficeAreaClass);
        return true;
    }

    @Override
    public OfficeAreaClassSingleGetOutput singleGet(Long id) {
        Optional<OfficeAreaClass> optionOfficeAreaClass = officeAreaClassRepository.findById(id);
        if(!optionOfficeAreaClass.isPresent()){
            throw exception(OFFICE_AREA_CLASS_NOT_EXIST);
        }
        return OfficeAreaClassConvert.INSTANCE.singleGetOutputConvert(optionOfficeAreaClass.get());

    }

    @Override
    public List<OfficeAreaClassListQueryOutput> listQuery() {
        List<OfficeAreaClass> listOfficeAreaClass = officeAreaClassRepository.listQuery();
        return OfficeAreaClassConvert.INSTANCE.listQueryOutputConvert(listOfficeAreaClass);

    }

}
